package com.easy.modules.sysmgr.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.easy.common.dao.IBaseDao;
import com.easy.common.util.BeanUtils;
import com.easy.common.util.date.DateUtils;
import com.easy.common.util.string.ClobUtil;
import com.easy.common.web.page.DataGrid;
import com.easy.common.web.page.Msg;
import com.easy.common.web.page.Pager;
import com.easy.modules.sysmgr.entity.ScheduleEntity;
import com.easy.modules.sysmgr.entity.UserEntity;
import com.easy.modules.sysmgr.service.ScheduleServiceI;
import com.easy.modules.sysmgr.web.form.ScheduleForm;

@Service
@Transactional
public class ScheduleServiceImpl implements ScheduleServiceI {

	@Autowired
	private IBaseDao<ScheduleEntity> basedaoSchedule;
	
	@Autowired
	private IBaseDao<UserEntity> basedaoUser;
	
	
	@Override
	public Msg save(ScheduleForm form) {
		try {
			ScheduleEntity entity = new ScheduleEntity();
			BeanUtils.copyNotNullProperties(form, entity, new String[] { "text", "start_date", "end_date", "start_date", "end_date" });
			entity.setStart_date(DateUtils.formatYYYYMMDD_HHMMSS(form.getStart_date())) ;
			entity.setEnd_date(DateUtils.formatYYYYMMDD_HHMMSS(form.getEnd_date())) ;
			entity.setText(ClobUtil.getClob(form.getText())) ;
			entity.setCreated(new Date()) ; 
			
			if(form.getUser_id() != null && !"".equals(form.getUser_id())) {
				entity.setUser(this.basedaoUser.load(UserEntity.class, form.getUser_id())) ;
			} else {
				return new Msg(false, "发生错误，无法找到用户的相关信息。") ;
			}
			
			this.basedaoSchedule.add(entity);
			
			return new Msg(true, "添加成功！") ;
		} catch (BeansException e) {
			e.printStackTrace() ;
			return new Msg(false, "程序发生错误！") ;
		}
	}

	@Override
	public Msg delete(ScheduleForm form) {
		try {
			this.basedaoSchedule.delete(ScheduleEntity.class, form.getId()) ;
			return new Msg(true, "删除成功！") ;
		} catch (BeansException e) {
			return new Msg(false, "程序发生错误！") ;
		}
	}
	
	@Override
	public Msg edit(ScheduleForm form) {
		try {
			ScheduleEntity entity = this.basedaoSchedule.load(ScheduleEntity.class, form.getId());
			
			if(entity != null) {
				form.setCreated(new Date()) ;
				BeanUtils.copyNotNullProperties(form, entity, new String[] { "text", "start_date", "end_date" });
				entity.setStart_date(DateUtils.formatYYYYMMDD_HHMMSS(form.getStart_date())) ;
				entity.setEnd_date(DateUtils.formatYYYYMMDD_HHMMSS(form.getEnd_date())) ;
				entity.setText(ClobUtil.getClob(form.getText())) ;
				
				this.basedaoSchedule.update(entity) ;
			}
			
			return new Msg(true, "编辑成功！") ;
		} catch (BeansException e) {
			return new Msg(false, "编辑失败！") ;
		}
	}
	

	@Override
	public ScheduleForm get(ScheduleForm form) {
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = "select t from ScheduleEntity t where 1=1";
		hql = addWhere(hql, form, params) ;
		
		ScheduleEntity entity = (ScheduleEntity) this.basedaoSchedule.queryObject(hql, params) ;
		if(null != entity) {
			ScheduleForm pform = new ScheduleForm();
			BeanUtils.copyNotNullProperties(entity, pform, new String[] { "text", "start_date", "end_date" });
			pform.setText(ClobUtil.getString(entity.getText())) ;
			pform.setStart_date(DateUtils.formatYYYYMMDD_HHMMSS(entity.getStart_date())) ;
			pform.setEnd_date(DateUtils.formatYYYYMMDD_HHMMSS(entity.getEnd_date())) ;
			
			return pform;
		} else {
			return null ;
		}
	}
	
	@Override
	public List<ScheduleForm> schedules(ScheduleForm form) {
		List<ScheduleForm> forms = new ArrayList<ScheduleForm>();
		Pager<ScheduleEntity> pager = this.find(form) ;
		
		if (null != pager && !pager.getDataRows().isEmpty()) {
			for (ScheduleEntity i : pager.getDataRows()) {
				ScheduleForm uf = new ScheduleForm();
				BeanUtils.copyNotNullProperties(i, uf, new String[] { "text", "start_date", "end_date" });
				uf.setText(ClobUtil.getString(i.getText())) ;
				uf.setStart_date(DateUtils.formatYYYYMMDD_HHMMSS(i.getStart_date())) ;
				uf.setEnd_date(DateUtils.formatYYYYMMDD_HHMMSS(i.getEnd_date())) ;
				forms.add(uf);
			}
		}
		return forms;
	}

	@Override
	public DataGrid datagrid(ScheduleForm form) {
		
		List<ScheduleForm> forms = new ArrayList<ScheduleForm>();
		Pager<ScheduleEntity> pager = this.find(form) ;
		if (null != pager && !pager.getDataRows().isEmpty()) {
			for (ScheduleEntity i : pager.getDataRows()) {
				ScheduleForm uf = new ScheduleForm();
				BeanUtils.copyNotNullProperties(i, uf, new String[] { "text", "start_date", "end_date" });
				uf.setText(ClobUtil.getString(i.getText())) ;
				uf.setStart_date(DateUtils.formatYYYYMMDD_HHMMSS(i.getStart_date())) ;
				uf.setEnd_date(DateUtils.formatYYYYMMDD_HHMMSS(i.getEnd_date())) ;
				forms.add(uf);
			}
		}
		
		DataGrid datagrid = new DataGrid();
		datagrid.setTotal(pager.getTotal());
		datagrid.setRows(forms);
		return datagrid;
	}


	private Pager<ScheduleEntity> find(ScheduleForm form) {
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = "select t from ScheduleEntity t where 1=1";
		hql = addWhere(hql, form, params) ;
		return this.basedaoSchedule.find(hql, params);
	}

	private String addWhere(String hql, ScheduleForm form, Map<String, Object> params) {
		if (form.getId() != null && !"".equals(form.getId())) {
			hql += " and t.id=:id";
			params.put("id", form.getId());
		}
		if (form.getUser_id() != null && !"".equals(form.getUser_id())) {
			hql += " and t.user.id=:user_id";
			params.put("user_id", form.getUser_id());
		}
		if(form.getMode() != null && !"".equals(form.getMode())) {
			if("day".equals(form.getMode())) {
				hql += " and date_format(t.start_date, '%Y-%m-%d')=:sd ";
				params.put("sd", form.getStart_date());
			} else if("week".equals(form.getMode())) {
				String[] array = DateUtils.convertWeekByDate(form.getStart_date()) ;
				hql += " and DATE_FORMAT(t.start_date,'%Y-%m-%d')>=:sd1 and DATE_FORMAT(t.start_date,'%Y-%m-%d')<=:sd2" ;
				params.put("sd1", array[0]);
				params.put("sd2", array[1]);
			} else if("month".equals(form.getMode())) {
				hql += " and DATE_FORMAT(t.start_date,'%Y-%m-%d')>=:sd1 and DATE_FORMAT(t.start_date,'%Y-%m-%d')<=:sd2" ;
				params.put("sd1", DateUtils.getFirstDayOfMonth(form.getStart_date()));
				params.put("sd2", DateUtils.getLastDayOfMonth(form.getStart_date()));
			} else if("year".equals(form.getMode())) {
				hql += " and DATE_FORMAT(t.start_date,'%Y-%m-%d')>=:sd1 and DATE_FORMAT(t.start_date,'%Y-%m-%d')<=:sd2" ;
				params.put("sd1", DateUtils.getFirstDayOfYear(form.getStart_date()));
				params.put("sd2", DateUtils.getLastDayOfYear(form.getStart_date()));
			}
		}
		return hql;
	}

}
